class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums)
    {
        int left = 0;
        int right = -1;
        int n = nums.size();

        int sum = 0;
        int res = n;
        int flag = 0;
        while (left < n)
        {
            while (right < n)
            {
                if (sum >= target)
                {
                    flag = 1;
                    break;
                }

                right++;
                if (right < n)
                {
                    sum += nums[right];
                }
            }

            if (right >= n)
            {
                break;
            }

            res = min(res, right - left + 1);
            sum -= nums[left++];
        }

        if (flag)
        {
            return res;
        }
        else
        {
            return 0;
        }
    }
};